﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Mvc.Mailer;
using RoccaStore.Services;
using RoccaStore.Utils;
using RoccaStore.Extensions;

namespace RoccaStore.Mailers
{
    public interface IUserMailer
    {
        MvcMailMessage Download(FileSend sendInfo);
        MvcMailMessage Confirmation(FileSend sendInfo);
    }

    public class UserMailer : Mvc.Mailer.MailerBase, IUserMailer
    {
        private readonly IFileInfoService _fileInfoService;

        public UserMailer(IFileInfoService fileInfoService)
        {
            _fileInfoService = fileInfoService;
        }

        public MvcMailMessage Download(FileSend sendInfo)
        {
            FileInfo file = null;

            if(sendInfo.Slug.FileID.HasValue)
                file = _fileInfoService.GetBySlug(sendInfo.Slug.Name);

            var mailMessage = new MvcMailMessage { Subject = "File notificaction: " + sendInfo.Slug.Title };

            if (!string.IsNullOrEmpty(sendInfo.To))
            {
                mailMessage.To.Add(sendInfo.To);
                mailMessage.From = new MailAddress(sendInfo.From);
                ViewBag.File = sendInfo.Slug.File;
                ViewBag.Slug = sendInfo.Slug;
                ViewBag.Files = sendInfo.Slug.Files;

                if (sendInfo.Slug.File != null)
                {
                    ViewBag.FormattedSize = sendInfo.Slug.File.FormattedSize;
                }
                else if (sendInfo.Slug.Files.Count > 0)
                {
                    long totalSize = 0;
                    sendInfo.Slug.Files.ToList().ForEach(g => {
                        var multiFile = _fileInfoService.GetByID(g.FileID.Value);
                        totalSize += multiFile.Size;
                    });
                    ViewBag.FormattedSize = totalSize.FormatBytes();
                } 

                if (sendInfo.Notify)
                {
                    ViewBag.HashedData = HashUtils.EncodeDynamicHash(new { ID = sendInfo.ID, Action = "Email" }, sendInfo.Slug.Key);
                }

                PopulateBody(mailMessage, viewName: "Download");
            }
            return mailMessage;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="to"></param>
        /// <param name="from"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public MvcMailMessage Confirmation(FileSend sendInfo)
        {
            var mailMessage = new MvcMailMessage { Subject = "File confirmation: " + sendInfo.Slug.Title };

            if (!string.IsNullOrEmpty(sendInfo.From))
            {
                mailMessage.To.Add(sendInfo.From);
                mailMessage.From = new MailAddress(sendInfo.To);
                ViewBag.Filename = sendInfo.Slug.Title;
                ViewBag.To = sendInfo.To;
                PopulateBody(mailMessage, viewName: "Confirmation");
            }
            return mailMessage;
        }
    }
}
